package com.jvxb.manage.livable.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.entity.AssetsCluster;
import com.jvxb.manage.livable.service.AssetsClusterService;
import com.jvxb.manage.livable.service.dbQuery.RedisQueryService;
import com.jvxb.manage.livable.vo.RedisQueryVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jvxb
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/cluster")
public class ClusterController {

    @Autowired
    private AssetsClusterService assetsClusterService;

    @GetMapping("one")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer")
    })
    public Object one(Integer id) {
        AssetsCluster assetsCluster = assetsClusterService.getById(id);
        return RespMsg.ok(assetsCluster);
    }

    @PostMapping("/add")
    public RespMsg add(@RequestBody AssetsCluster assetsCluster) {
        assetsClusterService.save(assetsCluster);
        return RespMsg.ok();
    }

    @PostMapping("/delete")
    public RespMsg delete(@RequestBody AssetsCluster assetsCluster) {
        assetsClusterService.removeById(assetsCluster);
        return RespMsg.ok();
    }

    @PostMapping("/update")
    public RespMsg update(@RequestBody AssetsCluster assetsCluster) {
        assetsClusterService.saveOrUpdate(assetsCluster);
        return RespMsg.ok();
    }

    @GetMapping("/list")
    public RespMsg list() {
        List<AssetsCluster> list = assetsClusterService.list(null);
        return RespMsg.ok(list);
    }

    @GetMapping("listPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "集群id/名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "dbType", value = "集群类型", dataType = "String", required = false),
            @ApiImplicitParam(name = "size", value = "每页条数，默认10条", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "current", value = "第几页,默认第一页", dataType = "Integer", required = false)
    })
    public RespMsg listPage(String name,
                            String dbType,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<AssetsCluster> assetsClusterIPage = assetsClusterService.selectPages(name, dbType, current, size);
        return RespMsg.ok(assetsClusterIPage);
    }

    @GetMapping("/getClusterMenu")
    public RespMsg getClusterMenu(Long clusterId, String dbType) {
        List<Map> clusterMenu = assetsClusterService.getClusterMenu(clusterId, dbType);
        return RespMsg.ok(clusterMenu);
    }
}
